package com.ycwl.library;


import android.service.credentials.CreateEntry;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import java.io.File;

public class ParseUIFromJson {
    public static class  NODE_TYPE {
         public static int VIEW = 1;
        public static int VIDEO = 2;
        public static int IMAGE = 4;
        public static int TEXT = 8;
        public static int SLIDE = 16;
    }


    public class BaseNode {
        public String id;
        public String name;
        public Number x;
        public Number y;
        public Number z;
        public Number width;
        public Number height;
        public int type;
        public String content;
        public Number total_time;
        public String url;
        public String mimetype;
        public String sha1;
        public BaseNode[] children;
        public String text_background_color;
        public String text_color;
        public Number text_size;
    }

    public class MProgram {
        public String id;
        public String name;
        public String description;
        public long effect_time;
        public long expiration_time;
        public Number total_time;
        public BaseNode view;
    }

    public class MPlayList {
        public String id;
        public String name;
        public String description;
        public Number total_time;
        public Number update_time;
        public MProgram[] programs;
    }
    public static final String TAG = "AutoUpdater";
    public static MPlayList parseUIFromJson(String jsonString) {
//        String jsonString = "{\"id\":\"a5fa7a44-7ce6-44f6-8365-8da486b84305\",\"name\":\"测试001\",\"total_time\":154.08177799999999,\"update_time\":123456789,\"description\":\"缩放\",\"programs\":[{\"id\":\"f964029d-2ba0-4215-bcb6-ac3ccd160c10\",\"name\":\"节目1\",\"total_time\":32.480889,\"description\":\"萨达理发卡饭卡打开到付件啊扣篮大赛发卡的发多少开发剑荡四方阿打卡机打个卡拉到发快递老师发来的咖啡机阿拉丁康师傅阿道夫考拉代发快递发了快递费阿迪力康师傅啊打开了发代付款啦饭卡代发考虑到发快递风扇\",\"view\":{\"id\":\"\",\"name\":\"new\",\"x\":0,\"y\":0,\"width\":1920,\"height\":540,\"type\":1,\"content\":\"\",\"total_time\":0,\"children\":[{\"id\":\"72f5a47a-1b8d-4619-a8c5-dd2b86ee64ec\",\"name\":\"new_72f5a47a-1b8d-4619-a8c5-dd2b86ee64ec\",\"x\":\"0\",\"y\":\"0\",\"width\":\"960\",\"height\":\"540\",\"type\":2,\"content\":\"ep1.mp4\",\"total_time\":32.480889,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/c6557b3b-7531-4a12-8b44-56867b1762e0\",\"mimetype\":\"video/mp4\",\"sha1\":\"134ac8e145914f22d1dc0d86e0401553910a5770\"},{\"id\":\"a0fb19e8-da27-48ff-8351-b4710c564028\",\"name\":\"new_a0fb19e8-da27-48ff-8351-b4710c564028\",\"x\":960,\"y\":0,\"width\":\"960\",\"height\":\"540\",\"type\":16,\"content\":\"滚屏组件\",\"total_time\":\"5\",\"children\":[{\"id\":\"b76c6648-40b5-4c36-815a-c08ed7c0dfb1\",\"name\":\"new_b76c6648-40b5-4c36-815a-c08ed7c0dfb1\",\"x\":0,\"y\":540,\"width\":\"960\",\"height\":\"540\",\"type\":4,\"content\":\"003.jpg\",\"total_time\":\"5\",\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/02633015-35f4-4753-9469-ea5fe8fde6fe\",\"mimetype\":\"image/jpeg\",\"sha1\":\"88d1558d30c350953e5d17bc2e6d2ce835ea0c01\"},{\"id\":\"4e7a8021-b412-494c-8d6f-06e1683d87c8\",\"name\":\"new_4e7a8021-b412-494c-8d6f-06e1683d87c8\",\"x\":0,\"y\":540,\"width\":\"960\",\"height\":\"540\",\"type\":2,\"content\":\"ep1.mp4\",\"total_time\":\"5\",\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/c6557b3b-7531-4a12-8b44-56867b1762e0\",\"mimetype\":\"video/mp4\",\"sha1\":\"134ac8e145914f22d1dc0d86e0401553910a5770\"},{\"id\":\"d09b459c-5ea3-4109-9839-3f4743f60e2f\",\"name\":\"new_d09b459c-5ea3-4109-9839-3f4743f60e2f\",\"x\":0,\"y\":0,\"width\":\"960\",\"height\":\"540\",\"type\":8,\"content\":\"文字啊李逵负荆狄拉克水电费阿斯卡立法法久啊离开的飞机啊老大发卡机老地方卡多发点发了快递费久啊离开的发收到反馈啦水电费阿斯顿发老师快递费阿斯卡两地分居啊塑料袋饭卡阿第三方阿里快递费就阿斯浪蝶狂蜂阿斯蒂芬f\",\"total_time\":\"5\",\"children\":[]},{\"id\":\"fef8e3fb-2a61-4210-a38d-9b9597ce58e2\",\"name\":\"new_fef8e3fb-2a61-4210-a38d-9b9597ce58e2\",\"x\":0,\"y\":0,\"width\":\"960\",\"height\":\"540\",\"type\":4,\"content\":\"001.jpg\",\"total_time\":\"10\",\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/0c70f896-5a99-4379-a21c-a0c189725360\",\"mimetype\":\"image/jpeg\",\"sha1\":\"d3c4c2d10be8778bf7808b2e56b629cd89ee4ee8\"},{\"id\":\"ffe24115-352a-4610-9b9d-870cd0cf3032\",\"name\":\"new_ffe24115-352a-4610-9b9d-870cd0cf3032\",\"x\":0,\"y\":0,\"width\":\"960\",\"height\":\"540\",\"type\":2,\"content\":\"ep1.mp4\",\"total_time\":0,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/c6557b3b-7531-4a12-8b44-56867b1762e0\",\"mimetype\":\"video/mp4\",\"sha1\":\"134ac8e145914f22d1dc0d86e0401553910a5770\"}]}]},\"effect_time\":1711900800,\"expiration_time\":1713369600},{\"id\":\"386ae380-341f-4e77-8bb9-25eebbadb617\",\"name\":\"节目三\",\"total_time\":32.480889,\"description\":\"发福利\",\"view\":{\"id\":\"\",\"name\":\"new\",\"x\":0,\"y\":0,\"width\":1920,\"height\":540,\"type\":1,\"content\":\"\",\"total_time\":0,\"children\":[{\"id\":\"a65cac97-6b47-4b19-99e3-0093ec4fb0ca\",\"name\":\"new_a65cac97-6b47-4b19-99e3-0093ec4fb0ca\",\"x\":\"0\",\"y\":\"0\",\"width\":200,\"height\":200,\"type\":2,\"content\":\"ep1.mp4\",\"total_time\":32.480889,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/d81cc560-2d46-4810-af19-45a4247dd2dd\",\"mimetype\":\"video/mp4\",\"sha1\":\"134ac8e145914f22d1dc0d86e0401553910a5770\"},{\"id\":\"903d5974-0624-4354-9cfd-3b9c81754136\",\"name\":\"new_903d5974-0624-4354-9cfd-3b9c81754136\",\"x\":419,\"y\":0,\"width\":\"960\",\"height\":\"540\",\"type\":4,\"content\":\"004.jpg\",\"total_time\":0,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/61d8c375-811c-470e-a7e9-3d2cb9e67f06\",\"mimetype\":\"image/jpeg\",\"sha1\":\"cf6644617e90cb7c72fca730b414a1d3a9de9145\"}]},\"effect_time\":1711900800,\"expiration_time\":1717084800},{\"id\":\"4e7d776b-998e-4d63-a3e6-caccbf7000c1\",\"name\":\"节目二\",\"total_time\":89.12,\"description\":\"发福利\",\"view\":{\"id\":\"\",\"name\":\"new\",\"x\":0,\"y\":0,\"width\":1920,\"height\":540,\"type\":1,\"content\":\"\",\"total_time\":0,\"children\":[{\"id\":\"b7141953-9c14-484f-9a72-e84320cab50d\",\"name\":\"new_b7141953-9c14-484f-9a72-e84320cab50d\",\"x\":\"0\",\"y\":\"0\",\"width\":\"960\",\"height\":\"540\",\"type\":2,\"content\":\"ep2.mp4\",\"total_time\":89.12,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/a0bf8c6c-ef0f-4764-8845-383c079f0b7d\",\"mimetype\":\"video/mp4\",\"sha1\":\"42598d13a48932f8ef2bcf414bafc5ff99a82f19\"},{\"id\":\"57208481-9f5a-442c-bd29-99ff29938e80\",\"name\":\"new_57208481-9f5a-442c-bd29-99ff29938e80\",\"x\":\"960\",\"y\":\"0\",\"width\":\"960\",\"height\":\"540\",\"type\":4,\"content\":\"002.jpg\",\"total_time\":0,\"children\":[],\"totalTime\":0,\"url\":\"blob:http://localhost:1818/f11e1128-3c1f-4baa-9c27-b4e3b60d8e55\",\"mimetype\":\"image/jpeg\",\"sha1\":\"2a51496f43a26a8e76419acb3c75ebd80abe868b\"}]},\"effect_time\":1711900800,\"expiration_time\":1714406400}]}";
        MPlayList mPlayList = null;
        try {
            Gson gson = new Gson();
            mPlayList = gson.fromJson(jsonString,MPlayList.class);
            if (mPlayList != null) {
                for (MProgram program: mPlayList.programs) {
                    initRealData(program.view);
                }
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return mPlayList;
    }

    private static void initRealData(BaseNode baseNode) {
        if (baseNode.type == NODE_TYPE.VIDEO || baseNode.type == NODE_TYPE.IMAGE) {
            AssetsListManager.Assets assets = AssetsListManager.getInstance().getAssetsByHash(baseNode.sha1);
            if (assets == null) {
                return;
            }
            baseNode.url = assets.path;
            assets.quote++;
        } else {
            for (BaseNode item :baseNode.children) {
                initRealData(item);
            }
        }
    }
}
